{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只能查询，其他操作和列表差不多（不可变）(最后面有可变扩展)\n",
    "test_tuple=(\"萌萌哒\",1,3,5,\"加息\",\"加息\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# count index\n",
    "print(test_tuple.count(\"加息\"))\n",
    "print(test_tuple.index(\"萌萌哒\"))#没有find方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "tuple.index(x): x not in tuple",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-1964eb3cc71a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 注意是左闭右开区间==>[1,4)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"加息\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;31m#查不到报错：ValueError: tuple.index(x): x not in tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: tuple.index(x): x not in tuple"
     ]
    }
   ],
   "source": [
    "# 注意是左闭右开区间==>[1,4)\n",
    "print(test_tuple.index(\"加息\", 1, 4))#查不到报错：ValueError: tuple.index(x): x not in tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "萌萌哒\n",
      "加息\n"
     ]
    }
   ],
   "source": [
    "#下标取\n",
    "print(test_tuple[0])\n",
    "print(test_tuple[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "萌萌哒\n",
      "1\n",
      "3\n",
      "5\n",
      "加息\n",
      "加息\n"
     ]
    }
   ],
   "source": [
    "# 遍历方式1\n",
    "for item in test_tuple:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "萌萌哒\n",
      "1\n",
      "3\n",
      "5\n",
      "加息\n",
      "加息\n"
     ]
    }
   ],
   "source": [
    "# 遍历方式2\n",
    "i=0\n",
    "while i<len(test_tuple):\n",
    "    print(test_tuple[i])\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "# 定义的扩展：\n",
    "test_tuple1=(1,) #(1)就不是元祖了\n",
    "test_tuple2=(2)\n",
    "print(type(test_tuple1))\n",
    "print(type(test_tuple2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2)\n",
      "(1, 2)\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 扩展：（后面讲字典遍历的时候会再提一下的）\n",
    "a=(1,2)\n",
    "b=a#把a的引用给b\n",
    "#a里面两个值,直接给左边两个变量赋值了（有点像拆包了）\n",
    "c,d=a #不是把a分别赋值给c和d，等价于：c=a[0] d=a[1]\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'萌萌哒'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 扩展：多维元组\n",
    "some_tuples=[(2,\"萌萌哒\"),(4,3)]\n",
    "some_tuples[0]\n",
    "some_tuples[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 扩展：可变的元组（元组在定义的时候就不能变了，但是可以通过类似这种方式来改变）\n",
    "value_tuple = (\"a\", \"1\", [\"mmd\"],{\"name\":\"dnt\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', '1', ['mmd'], {'name': 'dnt'})"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', '1', ['mmd', 'test'], {'name': 'dnt'})\n"
     ]
    }
   ],
   "source": [
    "value_tuple[2].append(\"test\")\n",
    "print(value_tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', '1', ['mmd', 'test'], {'name': 'dnt', 'wechat': 'dotnetcrazy'})\n"
     ]
    }
   ],
   "source": [
    "value_tuple[3][\"wechat\"]=\"dotnetcrazy\"\n",
    "print(value_tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
